package com.lj.ljminsu.detail.service.impl;


import com.github.wxpay.sdk.MyWXConfig;
import com.github.wxpay.sdk.WXPay;
import com.lj.ljminsu.detail.bean.OrderInfo;
import com.lj.ljminsu.detail.dao.UserOrderDao;
import com.lj.ljminsu.detail.service.UserOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

//订单业务
@Service
public class UserOrderServiceImpl implements UserOrderService {
    @Resource
    private UserOrderDao userOrderDao;

    /**
     * 添加订单
     * @param orderInfo
     * @return
     */
    @Transactional
    @Override
    public String updateOrder(OrderInfo orderInfo) throws Exception{
        //产生订单时间
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String payTime = dateFormat.format(date);//订单时间
        orderInfo.setPayTime(payTime);
        long time = date.getTime();
        orderInfo.setOrderNo(orderInfo.getUserId()+time);//设置订单号
        userOrderDao.updateOrder(orderInfo);


        //拉取微信支付的uri
        WXPay wxPay = new WXPay(new MyWXConfig());
        Map<String, String> data = new HashMap<String, String>();
        data.put("body", "旅家民宿");
        data.put("out_trade_no", orderInfo.getUserId()+time);
        data.put("device_info", "PC");
        data.put("fee_type", "CNY");
        data.put("total_fee", "1");
        data.put("spbill_create_ip", "123.12.12.123");
        //支付成功之后的回调接口
        //微信告诉微分销商户订单已付款
        data.put("notify_url", "http://zfsbhx.natappfree.cc/order/wxnotify");
        data.put("trade_type", "NATIVE");  // 此处指定为扫码支付
        data.put("product_id", "12");
        //下单
        Map<String, String> resp = wxPay.unifiedOrder(data);
        System.out.println(resp.get("code_url"));//resp.get("code_url")拉取微信支付后的二维码url
        return resp.get("code_url");
    }
}
